﻿<%@ WebHandler Language="C#" Class="UfcProblemDashBoard" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using ReportBusiness;
using System.Net;

/*
 * Description: 问题上报看板
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.7.21
 */

public class UfcProblemDashBoard : IHttpHandler
{
    private uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null && jObject["type"].ToString() == "getData")
        {
            try
            {
                Result r = getData(jObject);
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getDataSql(JObject jObject)
    {
        string sql = @"
SELECT f.factoryname,up.description,
       pb.productname,
       up.ufccontent,
       wc1.workcentername createWorkcenter,
       ce.fullname createEmployee,
       up.ufccreatedate,
       up.ufcchulidate,
       wc.workcentername resworkcentername,
       toe.fullname resfullname,
       DECODE(up.ufcstatus,1,'未处理',2,'处理中',3,'处理完') ufcstatus,
       up.ufcresult,
       CASE WHEN ROUND((SYSDATE-up.ufcchulidate),2) > 0 THEN ROUND((SYSDATE-up.ufcchulidate),2) ELSE 0 
       END  delayday
FROM ufcproblem up
LEFT JOIN ufcproblemtype upt ON upt.ufcproblemtypeid = up.ufcproblemtypeid
LEFT JOIN employee ce ON ce.employeeid = up.ufccreateemployeeid
LEFT JOIN resourcedef r ON r.resourceid = up.resourceid
LEFT JOIN sessionvalues sv ON sv.employeeid = ce.employeeid
LEFT JOIN workcenter wc1 ON wc1.workcenterid = sv.workcenterid
LEFT JOIN employee toe ON toe.employeeid = up.ufctoemployeeid
LEFT JOIN workcenter wc ON wc.workcenterid = up.workcenterid
LEFT JOIN factory f ON f.factoryid = up.factoryid
LEFT JOIN factory f1 ON f1.factoryid = wc.factoryid 
LEFT JOIN container c ON c.containerid = up.containerid 
LEFT JOIN product p ON p.productid = c.productid
LEFT JOIN productbase pb ON pb.productbaseid = p.productbaseid
WHERE up.ufcstatus <> 0
";
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND f.factoryid = '{0}'", jObject["factoryid"].ToString());
        }

        if (jObject["workcenterid"] != null && jObject["workcenterid"].ToString() != "")
        {
            sql += string.Format(" AND wc.workcenterid = '{0}'", jObject["workcenterid"].ToString());
        }

        //问题提出时间
        if (jObject["ufccreatedate"] != null && jObject["ufccreatedate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["ufccreatedate"].ToString());
            if (arr.Count == 2)
            {
                sql += string.Format(" AND up.ufccreatedate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
                sql += string.Format(" AND up.ufccreatedate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
            }
        }

        //状态
        if (jObject["status"] != null && jObject["status"].ToString() != "")
        {
            sql += string.Format(" AND up.ufcstatus = '{0}'", jObject["status"].ToString());
        }
        return sql;
    }

    Result getData(JObject jObject)
    {
        string sql = getDataSql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }
}